{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "79da83cf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-06T01:01:43.334938Z",
     "start_time": "2023-12-06T01:01:42.346960Z"
    },
    "execution": {
     "iopub.execute_input": "2024-01-05T03:21:12.284016Z",
     "iopub.status.busy": "2024-01-05T03:21:12.282672Z",
     "iopub.status.idle": "2024-01-05T03:21:12.709302Z",
     "shell.execute_reply": "2024-01-05T03:21:12.708435Z",
     "shell.execute_reply.started": "2024-01-05T03:21:12.283937Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np  \n",
    "import matplotlib.pyplot as plt  \n",
    "import cv2  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "03af82af-22a8-434f-9010-efa0a5ed220a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-05T03:27:46.413652Z",
     "iopub.status.busy": "2024-01-05T03:27:46.412338Z",
     "iopub.status.idle": "2024-01-05T03:27:46.426508Z",
     "shell.execute_reply": "2024-01-05T03:27:46.424691Z",
     "shell.execute_reply.started": "2024-01-05T03:27:46.413571Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[162, 162, 162, ..., 170, 155, 128],\n",
       "       [162, 162, 162, ..., 170, 155, 128],\n",
       "       [162, 162, 162, ..., 170, 155, 128],\n",
       "       ...,\n",
       "       [ 43,  43,  50, ..., 104, 100,  98],\n",
       "       [ 44,  44,  55, ..., 104, 105, 108],\n",
       "       [ 44,  44,  55, ..., 104, 105, 108]], dtype=uint8)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "13dbba49",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-06T01:01:43.699159Z",
     "start_time": "2023-12-06T01:01:43.337802Z"
    },
    "execution": {
     "iopub.execute_input": "2024-01-05T03:31:42.365740Z",
     "iopub.status.busy": "2024-01-05T03:31:42.364446Z",
     "iopub.status.idle": "2024-01-05T03:31:42.518106Z",
     "shell.execute_reply": "2024-01-05T03:31:42.517003Z",
     "shell.execute_reply.started": "2024-01-05T03:31:42.365662Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2076178/1087694513.py:10: RuntimeWarning: divide by zero encountered in log\n",
      "  dft_log = np.log(dft_shift)\n"
     ]
    }
   ],
   "source": [
    "# 读取图像  \n",
    "img = cv2.imread('../data/Lenna_(test_image).png')  \n",
    "img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "# 对图像进行DFT变换  \n",
    "dft = np.fft.fft2((img))  \n",
    "dft_shift = np.fft.fftshift(dft)  \n",
    "\n",
    "# 取对数，以便将数据范围压缩到可绘图的范围  \n",
    "img_log = np.log(img)  \n",
    "dft_log = np.log(dft_shift) \n",
    "\n",
    "# 展示原图像和DFT变换后的频谱图像  \n",
    "# plt.subplot(121), plt.imshow(img_log, cmap='gray')  \n",
    "# plt.title('Input Image'), plt.xticks([]), plt.yticks([])  \n",
    "# plt.subplot(122), plt.imshow(dft_log.real, cmap='gray')  \n",
    "# plt.title('DFT'), plt.xticks([]), plt.yticks([])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "80c0a222-0cf7-4cdd-a22d-db1771b8e5f7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-05T03:31:36.741155Z",
     "iopub.status.busy": "2024-01-05T03:31:36.739516Z",
     "iopub.status.idle": "2024-01-05T03:31:36.754677Z",
     "shell.execute_reply": "2024-01-05T03:31:36.752789Z",
     "shell.execute_reply.started": "2024-01-05T03:31:36.741064Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4.72445510e+07      +0.j        ,\n",
       "        -4.71332301e+05+1876870.62479729j,\n",
       "         1.91038331e+06-1639170.38493485j, ...,\n",
       "        -5.67695408e+05 -282091.34915407j,\n",
       "         1.91038331e+06+1639170.38493485j,\n",
       "        -4.71332301e+05-1876870.62479729j],\n",
       "       [ 3.93967860e+05-1682148.30311257j,\n",
       "        -2.58315119e+06+1195918.81981292j,\n",
       "        -9.31234095e+05+1048425.31604955j, ...,\n",
       "         5.76338763e+05 +263973.50338164j,\n",
       "        -4.56303006e+05 +987423.60618576j,\n",
       "         3.99407407e+05 +132394.65179874j],\n",
       "       [ 4.10282056e+03 -693774.33776684j,\n",
       "        -2.73863534e+05 +198286.57655668j,\n",
       "         4.76665735e+05 -242050.7723818j , ...,\n",
       "         3.78413283e+05 +607592.81492546j,\n",
       "        -3.95183054e+05 +161542.04218828j,\n",
       "         4.89820355e+05  -47852.09094851j],\n",
       "       ...,\n",
       "       [ 6.43063157e+05  +60101.07297588j,\n",
       "         5.21187718e+05 +594461.89369035j,\n",
       "        -2.01100352e+05 -309815.25196961j, ...,\n",
       "         9.03535389e+05 +231159.35716547j,\n",
       "         2.91443306e+05-1068782.2739399j ,\n",
       "        -3.55005051e+05 +809967.86990268j],\n",
       "       [ 4.10282056e+03 +693774.33776684j,\n",
       "         4.89820355e+05  +47852.09094851j,\n",
       "        -3.95183054e+05 -161542.04218828j, ...,\n",
       "        -7.88397192e+05 -798164.41689735j,\n",
       "         4.76665735e+05 +242050.7723818j ,\n",
       "        -2.73863534e+05 -198286.57655668j],\n",
       "       [ 3.93967860e+05+1682148.30311257j,\n",
       "         3.99407407e+05 -132394.65179874j,\n",
       "        -4.56303006e+05 -987423.60618576j, ...,\n",
       "        -4.46171704e+05+2317377.70613043j,\n",
       "        -9.31234095e+05-1048425.31604955j,\n",
       "        -2.58315119e+06-1195918.81981292j]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "b0284dc6-80cc-451a-9b7f-67d0885e4d45",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-05T03:31:59.959179Z",
     "iopub.status.busy": "2024-01-05T03:31:59.957569Z",
     "iopub.status.idle": "2024-01-05T03:31:59.973495Z",
     "shell.execute_reply": "2024-01-05T03:31:59.971514Z",
     "shell.execute_reply.started": "2024-01-05T03:31:59.959053Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[212720.            +0.j        ,   1962.14791566 +6526.26089898j,\n",
       "         -1041.220184    +244.29976451j, ...,\n",
       "          4348.54411847 +9711.0076308j ,  -1041.220184    -244.29976451j,\n",
       "          1962.14791566 -6526.26089898j],\n",
       "       [212720.            +0.j        ,   1962.14791566 +6526.26089898j,\n",
       "         -1041.220184    +244.29976451j, ...,\n",
       "          4348.54411847 +9711.0076308j ,  -1041.220184    -244.29976451j,\n",
       "          1962.14791566 -6526.26089898j],\n",
       "       [212720.            +0.j        ,   1962.14791566 +6526.26089898j,\n",
       "         -1041.220184    +244.29976451j, ...,\n",
       "          4348.54411847 +9711.0076308j ,  -1041.220184    -244.29976451j,\n",
       "          1962.14791566 -6526.26089898j],\n",
       "       ...,\n",
       "       [186517.            +0.j        , -13661.13541768+19591.36135432j,\n",
       "          3454.23846553-14873.89165484j, ...,\n",
       "          4222.32302203+13576.94707965j,   3454.23846553+14873.89165484j,\n",
       "        -13661.13541768-19591.36135432j],\n",
       "       [186910.            +0.j        , -13585.97720676+20036.00398748j,\n",
       "          3317.36103006-14928.83855534j, ...,\n",
       "          4104.08421864+13980.0335226j ,   3317.36103006+14928.83855534j,\n",
       "        -13585.97720676-20036.00398748j],\n",
       "       [186910.            +0.j        , -13585.97720676+20036.00398748j,\n",
       "          3317.36103006-14928.83855534j, ...,\n",
       "          4104.08421864+13980.0335226j ,   3317.36103006+14928.83855534j,\n",
       "        -13585.97720676-20036.00398748j]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft[:,:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "dcd0b8f8-53c9-407d-ad0c-4f12a49d5f3a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-05T03:31:01.287525Z",
     "iopub.status.busy": "2024-01-05T03:31:01.285895Z",
     "iopub.status.idle": "2024-01-05T03:31:01.300651Z",
     "shell.execute_reply": "2024-01-05T03:31:01.298946Z",
     "shell.execute_reply.started": "2024-01-05T03:31:01.287442Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7.13648321+3.14159265j, 5.7785359 +0.07677481j,\n",
       "        6.21776208-2.47373327j, ..., 7.42659311+0.93362463j,\n",
       "        6.21776208+2.47373327j, 5.7785359 -0.07677481j],\n",
       "       [6.11038693-2.52693796j, 7.20330702-1.98561227j,\n",
       "        6.36417394+2.04615906j, ..., 5.54957923-0.93538358j,\n",
       "        7.20764729+1.48825429j, 6.68693743+1.90117542j],\n",
       "       [6.58137999+0.65303529j, 6.322793  -2.70435306j,\n",
       "        6.69923889+0.08360643j, ..., 6.55459996+2.97244063j,\n",
       "        6.53071878-2.7582055j , 5.44651035+2.74843598j],\n",
       "       ...,\n",
       "       [5.46140113+2.11983822j, 6.81835721-2.0461605j ,\n",
       "        7.71239392-1.00989031j, ..., 6.93208453+0.11094419j,\n",
       "        6.95777845-0.77000474j, 5.36999815-1.31907486j],\n",
       "       [6.58137999-0.65303529j, 5.44651035-2.74843598j,\n",
       "        6.53071878+2.7582055j , ..., 7.25901654+1.77999413j,\n",
       "        6.69923889-0.08360643j, 6.322793  +2.70435306j],\n",
       "       [6.11038693+2.52693796j, 6.68693743-1.90117542j,\n",
       "        7.20764729-1.48825429j, ..., 6.84874948-2.71260982j,\n",
       "        6.36417394-2.04615906j, 7.20330702+1.98561227j]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft_log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fe9ebdbe-6928-4972-abba-eb8bf2fc4e5c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-05T03:26:02.009596Z",
     "iopub.status.busy": "2024-01-05T03:26:02.007914Z",
     "iopub.status.idle": "2024-01-05T03:26:02.020312Z",
     "shell.execute_reply": "2024-01-05T03:26:02.018757Z",
     "shell.execute_reply.started": "2024-01-05T03:26:02.009512Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(408.65425264853184-1907.3480380675542j)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft_shift[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "201337a9-c0f6-489f-99cf-bad248ec0269",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-05T03:26:14.891250Z",
     "iopub.status.busy": "2024-01-05T03:26:14.889585Z",
     "iopub.status.idle": "2024-01-05T03:26:14.903170Z",
     "shell.execute_reply": "2024-01-05T03:26:14.901449Z",
     "shell.execute_reply.started": "2024-01-05T03:26:14.891154Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1950.6344702500974"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(dft_shift[0][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "15b6a542",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-21T13:35:24.035402Z",
     "start_time": "2023-11-21T13:35:24.018438Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-601.7435495177888-192.75082482608695j)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft[255][255]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "370d5e91",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-21T13:41:33.847106Z",
     "start_time": "2023-11-21T13:41:33.839127Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-329738.631409614"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft.real[0][4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5cfc91ff",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-21T13:40:14.215809Z",
     "start_time": "2023-11-21T13:40:14.205832Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 0)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft.real.max()\n",
    "index = np.unravel_index(dft.argmax(), dft.shape)\t# 最大值索引\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3f713123",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-21T13:07:05.938104Z",
     "start_time": "2023-11-21T13:07:05.932130Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "512"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(dft_log)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "image",
   "language": "python",
   "name": "image"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
